import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Tag } from '#/interface/index'
import { useI18n } from 'vue-i18n'

export const useTagsStore = defineStore('tags', () => {
  const { t } = useI18n()
  const tagList = ref(
    JSON.parse(localStorage.getItem('tagList') || '[]').length > 0
      ? JSON.parse(localStorage.getItem('tagList') || '[]')
      : [{ name: t('layout.sideBar.home'), path: '/dashboard/workplace', closable: false }],
  )
  console.log(tagList.value)

  // 添加标签
  const addTag = (tag: Tag) => {
    if (tagList.value.find((item: Tag) => item.path === tag.path)) {
      return
    }
    tagList.value.push(tag)
    localStorage.setItem('tagList', JSON.stringify(tagList.value))
  }
  // 删除标签
  const removeTag = (tag: Tag) => {
    tagList.value = tagList.value.filter((item: Tag) => item.path !== tag.path)
    localStorage.setItem('tagList', JSON.stringify(tagList.value))
  }
  // 清空标签
  const clearTags = () => {
    tagList.value = [{ name: t('layout.sideBar.home'), path: '/dashboard/workplace', closable: false }]
    localStorage.setItem('tagList', JSON.stringify(tagList.value))
  }
  return {
    tagList,
    addTag,
    removeTag,
    clearTags,
  }
})
